/**
 * Validates, that the event entry time is set before the event start time.
 * 
 * @author Michael Serajnik
 */

package at.fhj.itm10.ticketmaster.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value = "ValidTimesValidator")
public class ValidTimesValidator implements Validator {

	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		
		UIInput enter = (UIInput) component.getAttributes().get("validenter");
        UIInput begin = (UIInput) component.getAttributes().get("validbegin");
        
        String enterValue = enter.getSubmittedValue().toString();
        String beginValue = begin.getSubmittedValue().toString();
        System.out.println("beg: "+beginValue);
        System.out.println("ent: "+enterValue);
        if(enterValue.equals("") || beginValue.equals("")) {
        	throw new ValidatorException(new FacesMessage("Die Beginn-Uhrzeit muss vor der Einlass-Uhrzeit liegen."));
        }
        else {
        
        	String[] splitEnterValue = enterValue.split(":");
        	String[] splitBeginValue = beginValue.split(":");
        
        	System.out.println(splitEnterValue[0]);
        	System.out.println(splitEnterValue[1]);
        	
        	System.out.println(splitBeginValue[0]);
        	System.out.println(splitBeginValue[1]);
		
        	int splitBeginValueHour = Integer.parseInt(splitBeginValue[0]);
        	int splitBeginValueMinutes = Integer.parseInt(splitBeginValue[1]);
        
        	int splitEnterValueHour = Integer.parseInt(splitEnterValue[0]);
        	int splitEnterValueMinutes = Integer.parseInt(splitEnterValue[1]);
        
        	System.out.println(splitBeginValueHour +"----"+splitEnterValueHour);
        	if(splitBeginValueHour < splitEnterValueHour) {
        		throw new ValidatorException(new FacesMessage("Die Beginn-Uhrzeit muss vor der Einlass-Uhrzeit liegen."));
        	}
        	else if(splitBeginValueHour == splitEnterValueHour) {
        	
        		if(splitBeginValueMinutes < splitEnterValueMinutes) {
        			throw new ValidatorException(new FacesMessage("Die Beginn-Uhrzeit muss vor der Einlass-Uhrzeit liegen."));
        		}
        		
        	}
        	
        }
		
	}

}
